/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.misc.cg.descriptor;

import org.vacoor.nothing.common.util.StringUtils;

/**
 * Many To One 关联
 *
 * @author vacoor
 */
public class ManyToOne extends ToOne {

    public ManyToOne(String source) {
        super(source);
    }

    @Override
    public String getComputedJoinColumn() {
        String relColumn = null;
        // 如果对方是维护方
        if (StringUtils.hasText(mappedBy)) {
            // 遍历所有关联表的 OneToMany 查找当前关联配置
            for (OneToMany rel : this.rel.getOneToMany()) {
                if (mappedBy.equals(rel.getProperty())) {
                    relColumn = rel.getJoinColumn();
                    break;
                }
            }

            if (!StringUtils.hasText(relColumn)) {
                throw new IllegalArgumentException(property + "标注为 ManyToOne mappedBy=" + mappedBy + "" + ", 但是不能在" + rel.getFullTypeName() + "中发现 OneToMany 属性:" + mappedBy + " @" + source);
            }
        } else {
            relColumn = getJoinColumn();
        }
        return relColumn;
    }
}
